using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using System;
using System.Collections.Generic;


namespace Framework.Resource {
    public sealed class ResourceManager : FrameworkModule
    {
        public override int Priority {get {return 60;}}

        public FTask<GameObject> LoadPrefabAsync(string prefabPath)
        {
            FTaskCompletionSource<GameObject> tcs = new FTaskCompletionSource<GameObject>();
            var asyncOperationHandle = Addressables.LoadAssetAsync<object>(prefabPath);
            asyncOperationHandle.Completed += (AsyncOperationHandle<object> handle)=>
            {
                try
                {
                    GameObject result = (GameObject)handle.Result;
                    tcs.SetResult(result);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception("LoadPrefab Error: " + prefabPath, e));
                }
            };
            return tcs.Task;
        }

    }
}